package com.sky.trade.service.impl;


import com.sky.api.client.ProductFeignClient;
import com.sky.api.domain.vo.DishVO;
import com.sky.api.domain.vo.SetmealVO;
import com.sky.common.context.BaseContext;
import com.sky.trade.domain.dto.ShoppingCartDTO;
import com.sky.trade.domain.entity.ShoppingCart;
import com.sky.trade.mapper.ShoppingCartMapper;
import com.sky.trade.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author toki
 */
@Service
@Slf4j
@RequiredArgsConstructor
//@EnableFeignClients(clients = {UserFeignClient.class, ProductFeignClient.class})
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private final ShoppingCartMapper shoppingCartMapper;
    private final ProductFeignClient productFeignClient;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车：{}", shoppingCartDTO);
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //只能查询自己的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());    // 设置当前用户id

        //判断当前商品是否在购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        if (shoppingCartList != null && shoppingCartList.size() == 1) {
            //如果已经存在，就更新数量，数量加1
            shoppingCart = shoppingCartList.get(0);   // 因为list只会存储一条数据,所以第一条数据即为当前商品
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {
            //如果不存在，插入数据，数量就是1

            //判断当前添加到购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                // 是菜品
                DishVO dish = productFeignClient.getDishById(dishId).getData();
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 是套餐
                SetmealVO setmeal = productFeignClient.getSetmealById(shoppingCartDTO.getSetmealId()).getData();
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);   // 数量默认为1
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return 购物车列表
     */
    public List<ShoppingCart> showShoppingCart() {
        return shoppingCartMapper.list(ShoppingCart.
                builder().
                id(BaseContext.getCurrentId()).
                build());
    }

    /**
     * 清空购物车
     */
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 购物车一个商品的数量减一
     *
     * @param shoppingCartDTO 购物车商品信息
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 将dto转换为entity
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 通过查询的方式设置对应的值(userId等),较为方便
        final List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size() > 0) shoppingCart = list.get(0);

        if (shoppingCart.getNumber() > 1) { // 若数量大于1,则直接数量减1
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {    // 若数量小于等于1,则直接删除该条记录
            shoppingCartMapper.deleteById(shoppingCart.getId());
        }
    }

}